// color
/*
 * Things to remember:
 * 1. Binary Search? DP? Greedy? Bitset?
 * 2. Find a conclusion?
 * 3. long long? Enough array size?
 * 4. Stay calm and focus on main points!
 * 5. CLEAR IN MULTI_TEST!!!
 * */
#include <cstdio>
#include <iostream>
#define ll long long
#define rep(i, s, t) for(int i=s; i<=t; ++i)
#define debug(x) cerr<<#x<<":"<<x<<"\n";
using namespace std;
const int N=2005;

int n, a[N]; ll f[N][N], g[N];

void solve()
{
	scanf("%d", &n);
	rep(i, 1, n) scanf("%d", a+i);
	rep(i, 1, n) rep(j, 1, n) f[i][j]=g[i]=0;
	ll res=0;
	rep(i, 1, n)
	{
		ll tmp=0;
		for(int j=i-2; j>0; j--)
		{
			if(j<i-2) tmp+=(a[j+1]==a[j+2])*a[j+1];
			f[i][1]=max(f[i][1], g[j]+tmp+(a[i]==a[j])*a[i]);
		}
		rep(j, 2, i) f[i][j]=max(f[i][j], f[i-1][j-1]+(a[i]==a[i-1])*a[i]);
		rep(j, 1, i)
		{
			res=max(res, f[i][j]);
			g[i]=max(g[i], f[i][j]+(a[i-j]==a[i+1])*a[i+1]);
		}
	}
	printf("%lld\n", res);
}

int main()
{
	freopen("color.in", "r", stdin);
	freopen("color.out", "w", stdout);
	int T; scanf("%d", &T);
	while(T--) solve();
	
	return 0;
}